package com.ysj.demo.accompanynetwork.activity;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.ysj.demo.accompanynetwork.ApiServiceUntil;
import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.util.SpUtil;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.adapter.ItemIntegralAdapter;
import com.ysj.demo.accompanynetwork.bean.IntegralRecordBean;
import com.ysj.demo.accompanynetwork.bean.IntegralRecordRequest;
import com.ysj.demo.accompanynetwork.bean.IntegralRecordResult;
import com.ysj.demo.accompanynetwork.bean.UserBean;
import com.ysj.demo.accompanynetwork.bean.UserIntegralResult;
import com.ysj.demo.accompanynetwork.util.ToolUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PlaymateCoinActivity extends BaseActivity {
    private TextView titleTv;
    private TextView userPointTv;
    private PullToRefreshLayout pullToRefreshLayout;
    private RecyclerView recyclerView;
    private ItemIntegralAdapter adapter;
    private List<IntegralRecordBean> list;

    private String userid;
    private int currentPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playmate_coin);
        StatusBarUtil.fullScreen(this);

        initView();
        initData();
    }

    private void initView() {
        list = new ArrayList<>();
        titleTv = findViewById(R.id.top_tv);
        titleTv.setText(getResources().getString(R.string.playmate_coin));
        userPointTv = findViewById(R.id.user_point_tv);
        recyclerView = findViewById(R.id.rec_lv);
        pullToRefreshLayout = findViewById(R.id.activity_main);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        adapter = new ItemIntegralAdapter(this, list);

        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter
        recyclerView.setAdapter(adapter);
//        //设置分割线
//        //添加自定义分割线
//        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
//        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider));
//        recyclerView.addItemDecoration(divider);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                loadTask(1, true);
                pullToRefreshLayout.finishRefresh();
            }

            @Override
            public void loadMore() {
                loadTask(currentPage, false);
                // 结束加载更多
                pullToRefreshLayout.finishLoadMore();
            }
        });
    }

    private void initData() {
        userid = (String) SpUtil.getParam(getApplicationContext(), SpUtil.USER_ID, "");
        loadTask(1, false);
        loadIntegralTask();
    }

    //获取列表
    public void loadTask(final int mCurrentPage, final boolean flag) {
        final IntegralRecordRequest bean = new IntegralRecordRequest();
        bean.setUserid(userid);
        bean.setPagesize(5);
        bean.setPagenum(mCurrentPage);
        bean.setType(0);
        Call<IntegralRecordResult> validCall = ApiServiceUntil.getInstance().getmApi().getIntegralRecord(bean);
        validCall.enqueue(new Callback<IntegralRecordResult>() {
            @Override
            public void onResponse(Call<IntegralRecordResult> call, Response<IntegralRecordResult> response) {
                IntegralRecordResult body = response.body();
                Log.i("PersonalCenterActivity", new Gson().toJson(body));
                if (body.getCode() == 0) {
                    if (mCurrentPage == 1) {
                        adapter.clearList();
                    }
                    ArrayList<IntegralRecordBean> list = body.getData().getRecords();
                    if (null != list && list.size() > 0) {
                        adapter.addList(list);
                        if (flag) {
                            ToolUtil.ToastShow(getApplication(), "刷新成功");
                        }
                    } else {
                        if (currentPage == 1) {
                            ToolUtil.ToastShow(getApplication(), "没有数据");
                        }
                        ToolUtil.ToastShow(getApplication(), "没有更多数据");
                    }
                    currentPage += 1;
                } else {
                    if (flag) {
                        ToolUtil.ToastShow(getApplication(), "刷新失败");
                    } else {
                        ToolUtil.ToastShow(getApplication(), "加载失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<IntegralRecordResult> call, Throwable t) {
                Log.i("PersonalCenterActivity", t.toString());
            }
        });
    }

    //获取列表
    public void loadIntegralTask() {
        UserBean bean = new UserBean(userid);
        Call<UserIntegralResult> IntegralCall = ApiServiceUntil.getInstance().getmApi().getUserIntegral(bean);
        IntegralCall.enqueue(new Callback<UserIntegralResult>() {
            @Override
            public void onResponse(Call<UserIntegralResult> call, Response<UserIntegralResult> response) {
                UserIntegralResult body = response.body();
                if (body.getCode() == 0) {
                    userPointTv.setText("" + body.getData().getUserpoints());
                } else {
                    userPointTv.setText("0");
                }
                Log.i("PersonalCenterActivity", new Gson().toJson(body));
            }

            @Override
            public void onFailure(Call<UserIntegralResult> call, Throwable t) {
                Log.i("PersonalCenterActivity", t.toString());
            }
        });
    }
}
